<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-yellow sidebar-mini">

<div class="modal fade" id="modal-modify" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                </button>
                <h4 class="modal-title" id="myModalLabel1">
                    驳回
                </h4>
            </div>
            <div class="modal-body">
            <form id="sheetForm1" class="form-horizontal">
                <input type="hidden" name="id">
                <div class="box-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">驳回原因</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control"  name="rejectReason" id = "rejectReason">
                        </div>
                    </div>
                </div>
            </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="btn-maintain1">
                    提交
                </button>
            </div>
        </div>
    </div>
</div>
<section class="content-header">
    <h1>外购件计划分配</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-proCode">项目编号</label>
                                    <input id="query-proCode" type="text" name="proCode" class="form-control"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label for="query-proName">项目名称</label>
                                    <input id="query-proName" type="text" name="proName" class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label for="query-malType">类型</label>
                                    <select id="query-malType" class="form-control" name="malType">
                                        <option value="" selected>请选择</option>
                                        <option th:each="type : ${special}" th:value="${type.name}"
                                                th:text="${type.name}"></option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-part">部分</label>
                                    <input id="query-part" type="text" name="part" class="form-control"
                                           placeholder="请输入部分">
                                </div>
                                <div class="form-group">
                                    <label for="query-sheetType">提报类型</label>
                                    <select id="query-sheetType" class="form-control" name="sheetType">
                                        <option value="" selected>请选择</option>
                                        <option value="1">提报单</option>
                                        <option value="2">变更单</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-status">执行状态</label>
                                    <select id="query-status" class="form-control" name="lssueStatus">
                                        <option value="">请选择</option>
                                        <option value="1" selected>未下达</option>
                                        <option value="2">已下达</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-branchName">提报科室</label>
                                    <input id="query-branchName" type="text" name="branchName" class="form-control"
                                           placeholder="请输入科室名称">
                                </div>
                                <div class="form-group">
                                    <label for="query-postName">提报人</label>
                                    <input id="query-postName" type="text" name="postName" class="form-control"
                                           placeholder="请输入提报人">
                                </div>

                                <!--                                <div class="form-group">-->
                                <!--                                    <label for="query-startTime">日期</label>-->
                                <!--                                    <input id="query-startTime" type="text" class="form-control datepicker" name="startTime"-->
                                <!--                                           readonly="readonly"/>-->
                                <!--                                    <label for="query-endTime"> 至 </label>-->
                                <!--                                    <input id="query-endTime" type="text" class="form-control datepicker" name="endTime"-->
                                <!--                                           readonly="readonly"/>-->
                                <!--                                </div>-->
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <!--                <div class="form-inline">-->
                <!--                    <div class="form-group">-->
                <!--                        <label for="query-unproNum">未下达计划项目数：</label>-->
                <!--                        <input id="query-unproNum" type="text" class="form-control"-->
                <!--                               style="border: none;width: 200Px;background-color: white" readonly-->
                <!--                        th:value="${sumNum.unproNum}">-->
                <!--                    </div>-->
                <!--                    <div class="form-group">-->
                <!--                        <label for="query-unplanNum">未下达计划数：</label>-->
                <!--                        <input id="query-unplanNum" type="text" class="form-control"-->
                <!--                               style="border: none;width: 300px;background-color: white" readonly-->
                <!--                        th:value="${sumNum.unplanNum}">-->
                <!--                    </div>-->
                <!--                </div>-->

                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table, typeName = ['', '外购件', '外协件', '原材料'], sheetType = ['', '提报单', '变更单'];

    $(function () {

        /** 加载列表 */
        table = js.table.init({
            url: ctx + "purch/list",
            showExport: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {
                    title: "分类", field: 'reportType', width: '60',
                    formatter: function (value) {
                        return typeName[value];
                    }
                },
                {
                    title: "项目编号", field: 'prodNo',

                    formatter: function (value, row) {
                        if (value) {
                            return '<span>' + value + '</span>';
                        } else {
                            return '<span>' + row.proCode + '</span>';

                        }
                    }
                },
                {title: "项目名称", field: 'proName', width: '250px'},
                {title: "项目数量", field: 'proNum'},
                {title: "类型", field: 'malType'},
                {title: "部分", field: 'part'},
                {
                    title: "提报类型", field: 'sheetType',
                    formatter: function (value) {
                        return value == 1 ? '提报单' : '变更单';
                    }
                },
                {title: "提报人", field: 'postName'},
                {title: "提报科室", field: 'branchName'},
                // {title: "计划数量", field: 'unplanNum'},
                {title: "提报日期", field: 'commitTime', width: '100px'},
                {
                    title: "执行状态", field: 'lssueStatus',
                    formatter: function (value) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未下达</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-success">已下达</span>';
                        }
                    }
                },
                {
                    title: "操作",
                    formatter: function (value, row) {
                        let actions = [];
                        if (row.sheetType == 2) {

                            if (row.prodNoNum > 1 && hasP("data:buy:edit") && row.lssueStatus == 1) {
                                actions.push('<a class="btn btn-warning btn-xs" onclick="changeSplit(' + js.jsonStringify(row) + ')"> 拆分</a> ');
                            }
                            if (row.prodNoNum == 1 && row.lssueStatus == 1 && hasP("data:buy:release")) {

                                actions.push('<a class="btn btn-primary btn-xs" onclick="chaPlanRelease(' + js.jsonStringify(row) + ')"> 下达</a> ');

                            }
                        }
                        if (row.prodNoNum > 1 && row.sheetType == 1 && hasP("data:buy:edit") && row.lssueStatus == 1) {
                            actions.push('<a class="btn btn-warning btn-xs" href="' + ctx + 'purch/split/' + row.id + '"> 拆分</a> ');
                        } else if (row.prodNoNum == 1 && row.sheetType == 1 && row.lssueStatus == 1 && hasP("data:buy:release")) {
                            <!--无下达按钮原因有生产令未创建情况，由于科长要求未创建生产令也能看到计划但不能下达而改-->
                            actions.push('<a class="btn btn-primary btn-xs" target="_blank" onclick="planRelease(this,' + js.jsonStringify(row) + ')"> 下达</a> ');
                        }
                        if (row.rawSplit == 1 && row.lssueStatus == 1 && hasP("data:buy:del")) {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delPlan(' + js.jsonStringify(row) + ')"> 删除</a>');
                        }
                        //添加退回按钮，未拆分的数据可以退回，拆分后的数据不可退回
                        if (row.rawSplit == 0 && row.lssueStatus == 1 && hasP("data:buy:repurn")) {
                            // actions.push('<a class="btn btn-danger btn-xs" onclick="chaReturn(' + js.jsonStringify(row) + ')"> 退回</a> ');
                            actions.push('<a class="btn btn-danger btn-xs" onclick="rejectReturn(' + js.jsonStringify(row) + ')">退回</a> ');
                        }
                        actions.push('<a class="btn btn-info btn-xs" onclick="selectDetail(' + js.jsonStringify(row) + ')">查看</a> ');
                        return actions.join('');
                    }
                }
            ]
        });

    });

    /** 查询提报计划的明细*/
    function selectDetail(row) {
        if (row.prodNo) {
            /*当拆分的生产令在查看时，给后台值传递生产令号，当生产令删除一个后在查看时也是生产令编号，会造成在提报相同项目计划时，查看数据多的情况
            这个问题解决不了，先拆分生产令，然后变更生产令，在删除一个生产令，变更后编号变回原来项目号，根本无法区分该项目是否拆分过生产令，太坑了。*/
            window.open(ctx + 'purch/selectResult?id=' + row.id + '&prodNo=' + row.prodNo);
        } else {
            window.open(ctx + 'purch/selectDetail?id=' + row.id + '&sheetType=' + row.sheetType);
        }
    }

    /** 计划下达*/
    function planRelease(btn,row) {
        let url;//访问的url
        let resultAndSheetId;//主表的是主表Id，输出表是输出表的Id
        let prodNo;//生产令号
        /*联查标记,1=sheet表数据，2=result表数据*/
        if (row.meter == 1) {
            url = ctx + "purch/sheetRelease";
            resultAndSheetId = row.id;
            prodNo = row.prodNo;
        } else if (row.meter == 2) {
            url = ctx + "purch/resultRelease";
            resultAndSheetId = row.resultId;
            prodNo = row.prodNo;
        }
        //点击按钮后进行锁定，待返回值后进行解锁
        $(btn).attr('disabled', true);
        js.confirmPost({
            content: "确定下达此任务吗?",
            url: url,
            async: false,
            data: {
                resultAndSheetId: resultAndSheetId,
                prodNo: prodNo,
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                    // refreshNum();
                } else {
                    js.modal.warning(result.msg);
                    js.table.refresh(table);
                }
            }
        });
        //解锁下达按钮
        $(btn).attr('disabled', false);
    }

    // function refreshNum(){
    //     js.get({
    //         url: ctx+"purch/refreshNum",
    //         success : function (result){
    //             if (result.type === web_status.SUCCESS) {
    //                 $("#query-unproNum").val(result.data.unproNum);
    //                 $("#query-unplanNum").val(result.data.unplanNum);
    //             } else {
    //                 js.modal.warning(result.msg);
    //             }
    //         }
    //     })
    //
    // }

    /** 修改备注模态框*/
    function rejectReturn(row){
        // document.getElementById("sheetForm1").reset();
        $("#sheetForm1").fillData(row);
        js.modal.open("modal-modify");
    }


    /** 修改备注*/
    $("#btn-maintain1").click(function () {
        let res = js.vd({
            id: 'sheetForm1',
            notEmpty: ['rejectReason']
        })
        if (!res) {
            return;
        }
        js.submit({
            formId: "sheetForm1",
            url: ctx + "purch/chaSheetReturn",
            data: new FormData($("#sheetForm1")[0]),
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.modal.hide("modal-modify");
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    })

    // /** 数据退回*/
    // function chaReturn(row) {
    //     let url;//访问的url
    //     url = ctx + "purch/chaSheetReturn";
    //     js.confirmPost({
    //         content: "确定退回此任务吗?",
    //         url: url,
    //         async: false,
    //         data: {
    //             row: JSON.stringify(row)
    //         },
    //         success: function (result) {
    //             if (result.type === web_status.SUCCESS) {
    //                     js.modal.success(result.msg)
    //                     js.table.refresh(table);
    //             } else {
    //                 js.modal.warning(result.msg);
    //             }
    //         }
    //     });
    // }

    /** 变更单的下达数据*/
    function chaPlanRelease(row) {
        let url;//访问的url
        /*联查标记,1=sheet表数据，2=result表数据*/
        if (row.meter == 1) {
            url = ctx + "purch/chaSheetRelease";
        } else if (row.meter == 2) {
            url = ctx + "purch/charesultRelease";
        }
        js.confirmPost({
            content: "确定下达此任务吗?",
            url: url,
            async: false,
            data: {
                row: JSON.stringify(row)
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    if (result.data == -2) {
                        js.modal.warning(result.msg)
                        js.table.refresh(table);
                    } else {
                        js.modal.success(result.msg)
                        js.table.refresh(table);
                    }
                    // refreshNum();
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });

    }

    function delPlan(row) {
        js.modal.confirm("确认删除吗？", function () {
            js.post({
                url: ctx + "purch/delPlan",
                data: {
                    prodNo: row.prodNo
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    function changeSplit(row) {

        js.post({
            url: ctx + 'purch/changeSplit/verify',
            data: {
                sheetId: row.sheetId
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    if (result.data == -1) {
                        js.modal.warning(result.msg)
                        js.table.refresh(table);
                    } else {
                        location.href = ctx + 'purch/changeSplit?id=' + row.id;
                    }
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }


</script>
</body>
</html>
